﻿using System;
using DAL;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data;
using Utils;
using Utils.enums;

namespace BLL
{
    
    public class LoginBLL
    {
        LoginDAL LoginDAL = new LoginDAL();

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public string Login(AdminInfo model)
        {
            if (!string.IsNullOrEmpty(model._adminID) && !string.IsNullOrEmpty(model._adminPWD))
            {
                DataTable dt = LoginDAL.Admin(model);
                if (dt.Rows.Count>0)
                {
                    return Loginlogic(model, dt);
                }
                else
                {
                    return EnumTools.GetDescription(LoginEnums.LOGIN_STATE.ACCOUNT_NOT);
                }
            }
            return EnumTools.GetDescription(LoginEnums.LOGIN_STATE.ACCOUNT_PWD_NOTNULL);
        }

        /// <summary>
        /// 判断密码是否正确
        /// </summary>
        /// <param name="model"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        private static string Loginlogic(AdminInfo model, DataTable dt)
        {
            string PWD = dt.Rows[0]["AdminPWD"].ToString();
            if (PWD.Equals(MD5.md5Encrypt(model._adminPWD)))
            {
                return EnumTools.GetDescription(LoginEnums.REGITER_STATES.SUCCEED);
            }
            else
            {
                return EnumTools.GetDescription(LoginEnums.LOGIN_STATE.PWD_MISTAKE);
            }
        }
    }
}
